package santiago.client;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import santiago.codebase.IClient;
import santiago.codebase.Notification;
import static santiago.codebase.Notification.ABOARD_GAME;
import static santiago.codebase.Notification.JOIN_GAME;
import static santiago.codebase.Notification.QUIT_GAME;
import static santiago.codebase.Notification.START_GAME;
import santiago.view.lobby.DialogJoinGame;

public class RMIClient extends UnicastRemoteObject implements IClient {

    protected RMIClient() throws RemoteException {
        super();
    }

    @Override
    public String getGameName() throws RemoteException {
        return Client.getGameName();
    }

    @Override
    public void notify(final Notification Notification) throws RemoteException {
        new Thread(new Runnable() {
            @Override
            public void run() {
                switch (Notification) {
                    case JOIN_GAME:
                        while (!(Client.getInterface().getPointer() instanceof DialogJoinGame)) {
                            Thread.yield();
                        }
                        Client.getInterface().refresh();
                        break;
                    case QUIT_GAME:
                        Client.getInterface().refresh();
                        break;
                    case START_GAME:
                        while (!(Client.getInterface().getPointer() instanceof DialogJoinGame)) {
                            Thread.yield();
                        }
                        Client.getInterface().refresh();
                        Client.getInterface().initGame();
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException ex) {
                        }
                        Client.getInterface().startGame();
                        break;
                    case ABOARD_GAME:
                        Client.getInterface().aboardGame();
                        break;
                    case PLAYER_ACTION:
                        Client.getInterface().refresh();
                        break;
                    case LAST_PLAYER_ACTION:
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException ex) {
                        }
                        Client.getInterface().refresh();
                        break;
                }
            }
        }).start();
    }

    @Override
    public void notify(final Notification Notification, final Object Data) throws RemoteException {
        new Thread(new Runnable() {
            @Override
            public void run() {
                switch (Notification) {
                    case PLAYER_MESSAGE:
                    case SYSTEM_MESSAGE:
                        Client.getInterface().displayMessage((String) Data);
                        break;
                }
            }
        }).start();
    }
}
